﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Entity;
using BAL;
using Common;
namespace Web
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadSchool();
        }

        protected void LoadSchool()
        {
            SchoolId.Items.Clear();
            IList<SchoolInfo> schoolinfos = new List<SchoolInfo>();
            schoolinfos = School.GetList();
            foreach (SchoolInfo info in schoolinfos)
            {
                SchoolId.Items.Add(new ListItem(info.Name, info.Id.ToString()));
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string _Password = "";
            if (Password.Text.Trim() == "")
            {
                Utility.ShowBoxyAlert("密码不能为空", this);
                return;
            }
            else if (RePassword.Text.Trim() == "")
            {
                Utility.ShowBoxyAlert("请重复输入密码", this);
                return;
            }
            else if (RePassword.Text.Trim() != Password.Text.Trim())
            {
                Utility.ShowBoxyAlert("两次密码输入不一样", this);
                return;
            }
            else
            {
                _Password = Password.Text.Trim();
            }
            string _Email = "";
            if (Email.Text.Trim() == "")
            {
                Utility.ShowBoxyAlert("Email不能为空", this);
                return;
            }
            else if(Users.CheckUserExistByEmail(Email.Text.Trim()))
            {
                Utility.ShowBoxyAlert("Email已存在", this);
                return;
            }
            else
            {
                _Email = Email.Text.Trim();
            }

            string _Name = "";
            if (Name.Text.Trim() == "")
            {
                Utility.ShowBoxyAlert("真实姓名不能为空", this);
                return;
            }
            else
            {
                _Name = Name.Text.Trim();
            }

            string _IdCard = "";
            if (IdCard.Text.Trim() == "")
            {
                Utility.ShowBoxyAlert("必须填写身份证", this);
                return;
            }
            else if (UserExtend.CheckUserExistIdCard(IdCard.Text.Trim()))
            {
                Utility.ShowBoxyAlert("身份证号码已存在", this);
                return;
            }
            else
            {
                _IdCard = IdCard.Text.Trim();
            }

            string _Address = "";
            if (Address.Text.Trim() == "")
            {
                Utility.ShowBoxyAlert("联系地址不能为空", this);
                return;
            }
            else
            {
                _Address = Address.Text.Trim();
            }
            string _Tel = "";
            if (Tel.Text.Trim() == "")
            {
                Utility.ShowBoxyAlert("联系电话不能为空", this);
                return;
            }
            else
            {
                _Tel = Tel.Text.Trim();
            }
            byte _Age = 0;
            if (Age.Text.Trim() == "")
            {
                Utility.ShowBoxyAlert("年龄不能为空", this);
                return;
            }
            else if (!PageValidate.IsNumber(Age.Text.Trim()))
            {
                Utility.ShowBoxyAlert("年龄只能是一个数字", this);
                return;
            }
            else
            {
                _Age = byte.Parse(Age.Text.Trim());
            }
            int _SchoolId = 0;
            if (SchoolId.SelectedValue.Trim() == "")
            {
                Utility.ShowBoxyAlert("请选择学校", this);
                return;
            }
            else if (!PageValidate.IsNumber(SchoolId.SelectedValue.Trim()))
            {
                Utility.ShowBoxyAlert("一般系统保护性错误", this);
                return;
            }
            else
            {
                _SchoolId = int.Parse(SchoolId.SelectedValue.Trim());
            }
            int _Business=0;
            if (Business.SelectedValue.Trim() == "")
            {
                Utility.ShowBoxyAlert("职务不能为空", this);
                return;
            }
            else if (!PageValidate.IsNumber(Business.SelectedValue.Trim()))
            {
                Utility.ShowBoxyAlert("职务必须是一个数字", this);
                return;
            }
            else
            {
                _Business = int.Parse(Business.SelectedValue.Trim());
            }
            int _Caste=0;
            if (Caste.SelectedValue.Trim() == "")
            {
                Utility.ShowBoxyAlert("职称不能为空", this);
                return;
            }
            else if (!PageValidate.IsNumber(Caste.SelectedValue.Trim()))
            {
                Utility.ShowBoxyAlert("职称必须是一个数字", this);
                return;
            }
            else
            {
                _Caste = int.Parse(Caste.SelectedValue.Trim());
            }
            string _Postcode = "";
            if (Postcode.Text.Trim() == "")
            {
                Utility.ShowBoxyAlert("邮政编码不能为空", this);
                return;
            }
            else if (!PageValidate.IsNumber(Postcode.Text.Trim()))
            {
                Utility.ShowBoxyAlert("邮政编码必须是一个数字", this);
                return;
            }
            else
            {
                _Postcode = Postcode.Text.Trim();
            }
            int _Sex=0;
            if (Sex.SelectedValue.Trim() == "")
            {
                Utility.ShowBoxyAlert("一般系统保护性错误", this);
                return;
            }
            else if (!PageValidate.IsNumber(Sex.SelectedValue.Trim()))
            {
                Utility.ShowBoxyAlert("一般系统保护性错误", this);
                return;
            }
            else
            {
                _Sex = int.Parse(Sex.SelectedValue.Trim());
            }
            string _Intro = "";
            _Intro = Intro.Text.Trim();

            UserInfo uinfo = new UserInfo();
            uinfo.Name = _Name;
            uinfo.PassWord = Utility.MD5(_Password);
            uinfo.PinYin = Module_String.GetPYString(_Name);
            uinfo.Email = _Email;
            uinfo.RoleId = 3;
            uinfo.Status = 1;
            uinfo = Users.Add(uinfo);
            UserExtendInfo ueinfo = new UserExtendInfo();
            ueinfo.Address = _Address;
            ueinfo.Age = _Age;
            ueinfo.Business = (byte)_Business;
            ueinfo.Caste = (byte)_Caste;
            ueinfo.Intro = _Intro;
            ueinfo.Postcode = _Postcode;
            ueinfo.SchoolId = _SchoolId;
            ueinfo.School = School.GetSingleInfo(_SchoolId).Name;
            ueinfo.Sex = (byte)_Sex;
            ueinfo.Tel = _Tel;
            ueinfo.UserId = uinfo.Id;
            ueinfo.IdCard = _IdCard;
            UserExtend.Save(ueinfo);
            Response.Redirect("login.aspx?regok=ok");
            //Utility.ShowBoxyConfirm("注册成功，您处于待审核状态，是否跳转到登陆界面？", "login.aspx", this);
            return;
        }
    }
}
